package game;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import org.newdawn.slick.tiled.TiledMap;

public class GameState  extends BasicGameState 
{
  public static final int ID = 1;	
  private Font font;
  private StateBasedGame game;
  public static int viewWidth = 400;
  public static int viewHeight = 240;
  public static float mapX = 0;
  public static float mapY = 0;
  public static TiledMap tmap;
  private Rectangle ctrlUp,ctrlDown,ctrlLeft,ctrlRight;
  private boolean goUp, goDown, goLeft, goRight;
  public int getID() 
  {
    return ID;
  }

  public void init(GameContainer container, StateBasedGame game) throws SlickException 
  {
    this.game = game;
    font = new AngelCodeFont("data/font/weasel.fnt","data/font/weasel.png");
    ctrlUp= new Rectangle(200,20,20,20);
    ctrlDown= new Rectangle(200,200,20,20);
    ctrlLeft= new Rectangle(20,120,20,20);
    ctrlRight= new Rectangle(350,120,20,20);
    loadMap("data/level0001.tmx");
  }

  public static void loadMap(String ref)
  {
    try {
      tmap = new TiledMap(ref, "data");
    } catch (SlickException e) {
      e.printStackTrace();
    }
  }


  public void update(GameContainer container, StateBasedGame game, int delta)
  {
    /*
    if (mapX > 0) {
      mapX = 0;
    }
    if (mapY > 0) {
      mapY = 0;
    }
    if (mapX<tmap.getWidth())
    {
      mapX=tmap.getWidth();
    }
    if (mapY<tmap.getHeight())
    {
      mapY=tmap.getHeight();
    }
    */
    if (goUp)
    {
      mapY+=1;
    }
    if (goDown)
    {
      mapY-=1;
    }
    if (goLeft)
    {
      mapX+=1;
    } 
    if (goRight && !(mapX<-(tmap.getWidth()*16)+400))
    {
      mapX-=1;
    }
  }

  public void render(GameContainer container, StateBasedGame game, Graphics g) 
  {
    g.scale(2, 2);
    g.setFont(font);
    g.drawString("blah",20,30);

    tmap.render(
        (int) mapX % 16,
        (int) mapY % 16,
        (int) (-mapX / 16),
        (int) (-mapY / 16),
        (viewWidth / 16) + 2,
        (viewHeight / 16) + 2,
        1, false);

    tmap.render(
        (int) mapX % 16,
        (int) mapY % 16,
        (int) (-mapX / 16),
        (int) (-mapY / 16),
        (viewWidth / 16) + 2,
        (viewHeight / 16) + 2,
        2, false);

    tmap.render(
        (int) mapX % 16,
        (int) mapY % 16,
        (int) (-mapX / 16),
        (int) (-mapY / 16),
        (viewWidth / 16) + 2,
        (viewHeight / 16) + 2,
        3, false);         

g.draw(ctrlUp);
g.draw(ctrlDown);
g.draw(ctrlLeft);
g.draw(ctrlRight);
g.drawString("mapX=("+mapX+"]    mapY=["+mapY+"]", 100, 100);
g.drawString("tmapWx16=["+tmap.getWidth()*16+"]", 100, 120);
    //tmap.render(0,0);
    g.resetTransform();
  }

  public void  mouseDragged(int oldx, int oldy, int newx, int newy) 
  {

  }

  public void mousePressed(int button, int x, int y)
  { 
    //game.enterState(TitleState.ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
    if (ctrlUp.contains(x/2,y/2))
    {
      goUp=true;
    }
    else if (ctrlDown.contains(x/2,y/2))
    {
      goDown=true;
    }
    else if (ctrlLeft.contains(x/2,y/2))
    {
      goLeft=true;
    }
    else if (ctrlRight.contains(x/2,y/2))
    {
      goRight=true;
    }
    else 
    {
      goUp=false;goDown=false;goLeft=false; goRight=false;
    }
  }

}
